<?php
/**
 * du 2024.10.12
 * 节目试玩版页面控制器
 */
namespace Home\Controller;
use Think\Controller;

class HavefunController extends Controller{
    public function __construct(){
        parent::__construct();
        $this->title = '嗨放派';
    }
    public function index(){
        // D('TvGameClueAdmin')->set_data(['id' => $id, 'status' => 'index']);
        $product = D('TvHavefunProduct')->get_data_list();
        $this->assign('product',$product);
        $this->display('index');
    }

    public function set_status(){
        $status = I('status');
        $balance = I('balance');
        $shop_status = ['status' => $status, 'balance' => $balance];
        S('havefun_shop_status', $shop_status);
    }

    public function get_status(){
        $shop_status = S('havefun_shop_status');
        _apiReturn(1,'获取成功', $shop_status);
    }

    public function next(){
        $id = I('param.id');
        D('TvGameClueAdmin')->set_data(['id' => $id, 'status' => 'next']);
        $this->display();
    }

    public function admin(){
        $product = D('TvHavefunProduct')->get_data_list();
        $shop_status = S('havefun_shop_status');
        $this->assign('shop_status',$shop_status);
        $this->assign('product',$product);
        $this->assign('cart', S('havefun_cart'));
        $this->display();
    }

    public function set_data(){
        $data = I('param.');
        if($data['status'] == 'reset'){
            S('havefun_cart', []);
        }
        else{
            S('havefun_shop_status', $data);
        }
        _apiReturn(1,'设置成功', $data);
    }

    public function get_client_status(){
        $heartbeat = S('havefun_heartbeat');
        if($heartbeat){
            $diff = time() - $heartbeat;
            if($diff < 10){
                $status = '在线';
            }else{
                $status = '离线';
            }
        }else{
            $status = '离线';
        }
        $cart_list = S('havefun_cart');
        if($cart_list){
            $cart_map = [
                1 => '短路线',
                2 => '闪送1个旗帜碎片',
                3 => '5人汽车代步1KM',
                4 => '3人汽车代步1KM',
                5 => '1人汽车代步1KM'
            ];
            foreach($cart_list as $k => $cart){
                $content = '';
                foreach($cart['cart'] as $k2 => $item){
                    $content .= $cart_map[$k2] . '：' . $item . ' 件；';
                }
                $cart_list[$k]['content'] = $content;
            }
        }
        _apiReturn(1,'获取成功', ['heartbeat_status' => $status, 'heartbeat' => date('Y-m-d H:i:s', $heartbeat), 'cart' => $cart_list, 'shop_status' => S('havefun_shop_status')]);
    }

    public function set_price(){
        $product_id = I('param.product_id');
        $price = I('param.price');
        $data = [
            'id' => $product_id,
            'price' => $price
        ];
        D('TvHavefunProduct')->set_data($data);
        _apiReturn(1,'设置成功', ['product_id' => $product_id, 'price' => $price]);
    }

    public function get_client_data(){
        // 记录客户端心跳包
        S('havefun_heartbeat', time());
        // 返回数据
        $data = S('havefun_shop_status');
        $shop_list = D('TvHavefunProduct')->get_data_list();
        foreach($shop_list as $item){
            $data['shop'][$item['id']] = [
                'id' => $item['id'],
                'title' => $item['title'],
                'price' => $item['price'],
                'limit' => $item['max'],
            ];
        }
        _apiReturn(1,'获取成功', $data);
    }

    public function set_cart(){
        $cart = I('cart');
        $cart['update_time'] = date('Y-m-d H:i:s', time());
        $current_cart = S('havefun_cart');
        if(!$current_cart){
            $current_cart = [];
        }
        array_push($current_cart, $cart);
        S('havefun_cart', $current_cart);
        S('havefun_heartbeat', time());
        $shop_status = S('havefun_shop_status');
        $shop_status['status'] = 'success';
        S('havefun_shop_status', $shop_status); // 标记为购买成功
        _apiReturn(1,'设置成功', $cart);
    }

    public function clue(){
        $id = I('param.id');
        $user = D('TvGameClueAdmin')->get_data($id);
        $clue = D('TvGameClue')->get_clue($user['clue']);
        $this->assign('clue',$clue);
        $this->display();
    }

    public function game(){
        $id = I('param.id');
        $user = D('TvGameClueAdmin')->get_data($id);
        $data = ['id' => $id];
        if($user['game'] == 3){
            $data['image'] = $user['image'] + 1;
        }
        if($user['status'] == 'next'){
            $data['status'] = 'game';
        }
        D('TvGameClueAdmin')->set_data($data);
        $this->display('game' . $user['game']);
    }

    function get_clue(){
        $id = I('param.id');
        $user = D('TvGameClueAdmin')->get_data($id);
        if($user['status'] == 'clue'){
            $user['clue'] = D('TvGameClue')->get_clue($user['clue']);
        }
        _apiReturn(1,'获取成功', $user);
    }

    public function get_data_list(){
        $list = D('TvGameClueAdmin')->get_data_list();
        _apiReturn(1,'获取成功', $list);
    }

    public function set_game_success(){
        $id = I('id');
        $res = D('TvGameClueAdmin')->set_data(['id' => $id,'status' => 'win']);
        _apiReturn(1,'修改成功', $res);
    }

    public function add_clue(){
        $data = I('param.');
        $data['type'] = 'text';
        $res = M('TvGameClue')->data($data)->add();
        _apiReturn(1,'添加成功', $res);
    }

    public function checkbox(){
        $list = range(1,100);
        $this->assign('list',$list);
        $this->display();
    }

    public function diff(){
        $user = D('TvGameClueAdmin')->get_data(I('id'));
        $page = $user['image'] % 4 + 1;
        $image_list = D('TvGameImage')->get_image_list($page);
        $this->assign('image_list',$image_list);
        $this->display();
    }
}